﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using TfsUtils.Ui.Mvvm.Ui;
using TfsUtils.Utils;


namespace TfsUtils.Ui.Mvvm
{
    public static class MvvmHelpers
    {
        public static void Set<T, U>(this T instance, Expression<Func<T, U>> action, U newValue, ref U backingField)
          where T : UiObjectBase
        {
            if ((backingField == null) && (newValue == null)) return;
            if ((backingField != null) && (newValue.Equals(backingField))) return;
            String name = action.GetMemberName();
            backingField = newValue;
            instance.OnPropertyChanged(name);
        }

 

        public static void AddError<T>(this T uiObject, Expression<Func<T, Object>> action, String error)
            where T : UiObjectBase 
        {
            uiObject.AddError(action.GetMemberName(), error);
        }
    }
}
